Skip to contents

Converts a model_spec to a formula object. Unlike stats::reformulate, this function handles complex response strings (e.g. "Surv(time, event)", "log(y)") correctly by parsing them as R expressions rather than coercing them to symbols. Parts set with set_parts are included after |.

Usage

to_formula(spec, response = NULL)

Arguments

spec

A model_spec object.

response

Optional character string for the response variable. If supplied, returns a two-sided formula response ~ terms. May be a plain variable name or a complex expression such as "Surv(time, event)" or "log(y)".

Value

A formula object.

Examples

to_formula(model_spec(cyl, disp))
#> ~cyl + disp
#> <environment: 0x62b099dd4088>
to_formula(model_spec(cyl, disp), response = "mpg")
#> mpg ~ cyl + disp
#> <environment: 0x62b099ba7778>
to_formula(model_spec(cyl, disp), response = "Surv(time, event)")
#> Surv(time, event) ~ cyl + disp
#> <environment: 0x62b099b62300>
to_formula(model_spec(cyl, intercept = FALSE), response = "mpg")
#> mpg ~ 0 + cyl
#> <environment: 0x62b099b198d0>